package petstore.web.servlet.signup;

import petstore.persistence.user.UserDAOImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/usernameExit")
public class usernameExitServlet extends HttpServlet {
    private String username;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username = req.getParameter("username");//获取传递过来的用户名
        System.out.println(username);
        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();
        if(isExit(username)){
            out.print("exit");
        }else out.print("No");
    }

    boolean isExit(String username){
        UserDAOImpl userDAO = new UserDAOImpl();
        return userDAO.FindUserByUsername(username);//查找
    }
}
